#include "player.h"
#include <QKeyEvent>
#include <QDebug>

Player::Player(QGraphicsItem *parent)
    : GameObject(parent), jumping(false), velocity(0),
    gravity(0.3), jumpForce(-12), groundLevel(300)
{
    setPixmap(QPixmap(":/images/D:/player.jpg").scaled(50, 50));
    setPos(100, groundLevel-pixmap().height());
}

void Player::update()
{
    if(jumping) {
        setPos(x(), y() + velocity);
        velocity += gravity;

        if(y() >= groundLevel) {
            setPos(x(), groundLevel);
            jumping = false;
            velocity = 0;
        }
    }
    if(jumping){
        qDebug()<<"Jumping-Pos:"<<y()<<"Velocity:"<<velocity;
    }
}

void Player::keyPressEvent(QKeyEvent *event) {
    if(event->key() == Qt::Key_Space && !jumping) {
        qDebug() << "Jump triggered!"; // 调试输出
        jumping = true;
        velocity = jumpForce;
    }
}
